﻿using System;
using System.Collections.Generic;
using System.Linq;
using Karma.Framework.Core.Domain.Tools.Import.Extension;

namespace Karma.Framework.Core.Domain.Tools.Import.Configuration.Fluent
{
    public class ImporterConfiguration
    {
        protected IList<EntityConfiguration> _importableTypes = new List<EntityConfiguration>();
        public EntityConfiguration[] ImportableTypes
        {
            get
            {
                return _importableTypes.ToArray();
            }
        }

        protected IList<Type> _excludedTypes = new List<Type>();
        public Type[] ExcludedTypes
        {
            get
            {
                return _excludedTypes.ToArray();
            }
        }

        protected IList<Type> _listeners = new List<Type>();
        public Type[] Listeners
        {
            get
            {
                return _listeners.ToArray();
            }
        }

        protected FluentEntityConfiguration<T> AddEntity<T>() where T : DomainObject
        {
            var econf = new FluentEntityConfiguration<T>();
            _importableTypes.Add(econf.Configuration);
            return econf;
        }

        protected FluentTransientConfiguration<T> AddTransient<T>()
        {
            var econf = new FluentTransientConfiguration<T>();
            _importableTypes.Add(econf.Configuration);
            return econf;
        }

        protected void ExcludeFromHierarchy<T>() where T : DomainObject
        {
            _excludedTypes.Add(typeof(T));
        }

        protected Type _persister;
        protected ImporterConfiguration Persister<T>()
        {
            _persister = typeof(T);
            return this;
        }

        public Type CustomPersister
        {
            get
            {
                return _persister;
            }
        }

        protected FluentImporterConfiguration With
        {
            get
            {
                return new FluentImporterConfiguration(this);
            }
        }

        public class FluentImporterConfiguration
        {
            private ImporterConfiguration configuration;

            public FluentImporterConfiguration(ImporterConfiguration config)
            {
                configuration = config;
            }

            public void Listener<T>() where T : IEntityPersisterListener
            {
                configuration._listeners.Add(typeof(T));
            }

        }
    }

}
